﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.VehicleSuiteBusinessTemplate;
using BCDto.TMS.VehicleSuiteBusinessTemplate;
using BCEntity.TMS.VehicleSuiteBusinessTemplate;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.TMS.VehicleTransportBusinessConfig;
using BCDto.Sim.Admin.Admin;

namespace BCService.TMS.VehicleSuiteBusinessTemplate
{
	public class VehicleSuiteBusinessTemplateService : IVehicleSuiteBusinessTemplateService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IVehicleSuiteBusinessTemplateData vehicleSuiteBusinessTemplateData;
		private readonly IVehicleTransportBusinessConfigData vehicleTransportBusinessConfigData;
		private readonly string key = string.Format("{0}", EntityNameConst.VehicleSuiteBusinessTemplateEntity);

		public VehicleSuiteBusinessTemplateService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IVehicleSuiteBusinessTemplateData vehicleSuiteBusinessTemplateData,
			IVehicleTransportBusinessConfigData vehicleTransportBusinessConfigData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.vehicleSuiteBusinessTemplateData = vehicleSuiteBusinessTemplateData;
			this.vehicleTransportBusinessConfigData = vehicleTransportBusinessConfigData;
		}

		#region 检查车辆套餐业务模板是否存在
		public bool Exists(int vehicleSuiteBusinessTemplateId)
		{
			return this.vehicleSuiteBusinessTemplateData.Exists(vehicleSuiteBusinessTemplateId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加车辆套餐业务模板
		public VehicleSuiteBusinessTemplateDto Add(AdminDto adminDto,VehicleSuiteBusinessTemplateRequestDto requestDto)
		{
            if (adminDto.AgentRecord != null)
            {
				throw new ArgumentException("代理商无权配置此模式模板");
            }
			var vehicleTransportBusinessConfigEntity = this.vehicleTransportBusinessConfigData.GetEntity(requestDto.VehicleTransportBusinessConfigId).Result;
            if (vehicleTransportBusinessConfigEntity == null)
            {
				throw new ArgumentException("运输业务配置信息不存在!");
            }
            if (vehicleSuiteBusinessTemplateData.Exists(requestDto.VehicleTransportBusinessConfigId, requestDto.SuiteBusinessType).GetAwaiter().GetResult())
            {
				throw new ArgumentException("此套餐配置已存在!");
            }

			var entity = requestDto.As<VehicleSuiteBusinessTemplateEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;

			return this.vehicleSuiteBusinessTemplateData.Add(entity).GetAwaiter().GetResult().As<VehicleSuiteBusinessTemplateDto>();
		}
		#endregion

		#region 修改车辆套餐业务模板
		public VehicleSuiteBusinessTemplateDto Update(AdminDto adminDto, VehicleSuiteBusinessTemplatePutDto putDto)
		{
			if (!this.Exists(putDto.VehicleSuiteBusinessTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var vehicleTransportBusinessConfigEntity = this.vehicleTransportBusinessConfigData.GetEntity(putDto.VehicleTransportBusinessConfigId).Result;
			if (vehicleTransportBusinessConfigEntity == null)
			{
				throw new ArgumentException("运输业务配置信息不存在!");
			}
			var checkEntity = vehicleSuiteBusinessTemplateData.GetEntity(putDto.VehicleTransportBusinessConfigId, putDto.SuiteBusinessType).GetAwaiter().GetResult();
			if(checkEntity.VehicleSuiteBusinessTemplateId != putDto.VehicleSuiteBusinessTemplateId)
			{
				throw new ArgumentException("此套餐配置已存在!");
			}
			var entity = putDto.As<VehicleSuiteBusinessTemplateEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			var result = this.vehicleSuiteBusinessTemplateData.Update(entity).GetAwaiter().GetResult().As<VehicleSuiteBusinessTemplateDto>();
			string cacheKey = this.redisService.GetKey(key, entity.VehicleSuiteBusinessTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除车辆套餐业务模板
		public bool Delete(int vehicleSuiteBusinessTemplateId)
		{
			if (!this.Exists(vehicleSuiteBusinessTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.vehicleSuiteBusinessTemplateData.Delete(vehicleSuiteBusinessTemplateId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, vehicleSuiteBusinessTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取车辆套餐业务模板
		public VehicleSuiteBusinessTemplateDto Get(int vehicleSuiteBusinessTemplateId)
		{
			string cacheKey = this.redisService.GetKey(key, vehicleSuiteBusinessTemplateId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.vehicleSuiteBusinessTemplateData.GetEntity(vehicleSuiteBusinessTemplateId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<VehicleSuiteBusinessTemplateDto>();
		}
		#endregion

		#region 获取车辆套餐业务模板列表
		public IEnumerable<VehicleSuiteBusinessTemplateDto> GetList()
		{
			var result = this.redisService.TryGet(key, () =>
			{
				return this.vehicleSuiteBusinessTemplateData.GetList().GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<VehicleSuiteBusinessTemplateDto>>();
		}
		#endregion

		#region 获取车辆套餐业务模板分页
		public PageableList<VehicleSuiteBusinessTemplateDto> Query(string suiteBusinessName, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key,suiteBusinessName, enabled, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.vehicleSuiteBusinessTemplateData.Query(suiteBusinessName, enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<VehicleSuiteBusinessTemplateDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<VehicleSuiteBusinessTemplateDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion

		#region 启用车辆套餐业务模板
		public bool Enabled(AdminDto adminDto, int vehicleSuiteBusinessTemplateId)
		{
			if (!this.Exists(vehicleSuiteBusinessTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.vehicleSuiteBusinessTemplateData.Enabled(vehicleSuiteBusinessTemplateId,adminDto.AdminId,adminDto.FullName).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, vehicleSuiteBusinessTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 禁用车辆套餐业务模板
		public bool Disabled(AdminDto adminDto, int vehicleSuiteBusinessTemplateId)
		{
			if (!this.Exists(vehicleSuiteBusinessTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.vehicleSuiteBusinessTemplateData.Disabled(vehicleSuiteBusinessTemplateId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, vehicleSuiteBusinessTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion
	}
}